package com.gw2.gw2world.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.gw2.gw2world.R;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.ArcShape;
import android.graphics.drawable.shapes.OvalShape;

public class CircularProgressBar implements Serializable {
	private static final long serialVersionUID = 1L;
	public static final boolean HOME_ICON=true;
	public static final boolean GUEST_ICON=false;
	private Context context;
	private LayerDrawable renderedItem;
	private List<Drawable> items;
	//private int[] colors=new int[]{};
	/** Number of item in circle
	 * @param n
	 */
	public CircularProgressBar(Context c, int n, boolean whichIcon) {
		context=c;
		items=new ArrayList<Drawable>(n+3);
		float radius=360/n;
		//background color (separator between arcs)
		ShapeDrawable sd=new ShapeDrawable(new OvalShape());
		sd.getPaint().setColor(Color.WHITE);
		sd.setIntrinsicWidth(64);
		sd.setIntrinsicHeight(64);
		items.add(sd);
		//arcs
		for(int i=1; i<=n; i++) {
			sd=new ShapeDrawable(new ArcShape(radius*(i-1)-90, radius-2));
			sd.getPaint().setColor(Color.GRAY);
			if(i==n) sd.getPaint().setColor(0xffc0c0c0);
			sd.setIntrinsicWidth(64);
			sd.setIntrinsicHeight(64);
			items.add(sd);
		}
		//center hole
		sd=new ShapeDrawable(new OvalShape());
		sd.getPaint().setColor(Color.BLACK);
		items.add(sd);
		//add home/guest icon
		if(whichIcon==HOME_ICON) items.add(context.getResources().getDrawable(R.drawable.home));
		else items.add(context.getResources().getDrawable(R.drawable.guest));
	}
	public LayerDrawable getDrawable() {
		renderedItem=new LayerDrawable(items.toArray(new ShapeDrawable[items.size()]));
		renderedItem.setLayerInset(items.size()-1, 20, 20, 20, 20); //resize center hole
		renderedItem.setLayerInset(items.size()-2, 10, 10, 10, 10); //resize center hole
		items.get(items.size()-1).setAlpha(128);
		return renderedItem;
	}
	public void setSeparatorColor(int color) {((ShapeDrawable)items.get(0)).getPaint().setColor(color);}
	public void setProgression(int progression, int color) {
		for(int i=1; i<items.size()-2; i++)
			if(i<=progression) ((ShapeDrawable)items.get(i)).getPaint().setColor(color);
			else ((ShapeDrawable)items.get(i)).getPaint().setColor(Color.GRAY);
	}
	public void setColor(int section, int color) {
		if(section>=0 && section<items.size()-2) ((ShapeDrawable)items.get(section)).getPaint().setColor(color);
	}
}
